// 会员等级模型
const { DataTypes } = require("sequelize");
const sequelize = require("../config/sequelize.js");

const MembershipLevel = sequelize.define("membership_level", {
  id: {
    type: DataTypes.STRING(20),
    primaryKey: true,
    allowNull: false
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false
  },
  color: {
    type: DataTypes.STRING(20),
    allowNull: false
  },
  privileges: {
    type: DataTypes.TEXT,
    allowNull: false,
    get() {
      const privileges = this.getDataValue('privileges');
      try {
        return JSON.parse(privileges);
      } catch (e) {
        return [];
      }
    },
    set(value) {
      this.setDataValue('privileges', JSON.stringify(value));
    }
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: true
  },
  updatedAt: {
    type: DataTypes.DATE,
    allowNull: true
  }
}, {
  tableName: "membership_levels",
  timestamps: true
});

module.exports = MembershipLevel;